#!/bin/sh

EMBOX_BIN=${1:-"./build/base/bin/embox.bin"}
UIMAGE=${2:-uImage}
MKIMAGE=$(which mkimage)

if [ ! "$MKIMAGE" ]; then
	echo "!!!!!!!  ERROR !!!!!!!!!!!!! please install mkimage or uboot-tools"   >&2
	exit 1
fi

case $OMAP_UBOOT_IMAGE_BOARD in
	ti8168|overo)
		LOAD_ADDR=0x81000000
		;;
	omapl137)
		LOAD_ADDR=0xc0700000
		;;
	bananapi)
		LOAD_ADDR=0x43000000
		;;
	*)
		LOAD_ADDR=0x80000000
		;;
esac
ENTRY_ADDR=$(printf "0x%x" $(($LOAD_ADDR + 0x8040)))

echo LOAD_ADDR=$LOAD_ADDR ENTRY_ADDR=$ENTRY_ADDR

PAD_FILE_NAME=".pad.zero"
PADDED_BIN=".embox-padded.bin"

PAD_SIZE=32K

dd if=/dev/zero of="$PAD_FILE_NAME" bs=1 count=$PAD_SIZE

cat "$PAD_FILE_NAME" "$EMBOX_BIN" > "$PADDED_BIN"

"$MKIMAGE" -A arm -O linux -C none -T kernel -a $LOAD_ADDR -e "$ENTRY_ADDR" -d "$PADDED_BIN" "$UIMAGE"

rm "$PAD_FILE_NAME" "$PADDED_BIN"
